package net.monare.mifss.evn;

import net.monare.mifss.MifssHolder;
import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.mod.PoolDefUiModule;
import net.monare.mifss.admin.ui.win.ConfigureStoragePoolWindow;
import net.monare.mifss.admin.ui.win.ConfigureStorageWindow;
import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.StorageServiceException;

import com.vaadin.ui.Notification;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class UpdatePoolDefinitionListener extends BasicMifssListener implements ClickListener {

	private static final long serialVersionUID = 8610696543724279345L;

	private PoolDefUiModule poolDefUiModule;
	private StorageDef storageDef;
	private ConfigureStorageWindow parent;
    private ConfigureStoragePoolWindow parentWindow;



	public UpdatePoolDefinitionListener(StorageDef storageDef, PoolDefUiModule poolDefUiModule, ConfigureStorageWindow parent, ConfigureStoragePoolWindow parentWindow) {

		this.storageDef = storageDef;
		this.poolDefUiModule = poolDefUiModule;
		this.parent = parent;
        this.parentWindow = parentWindow;
	}

	@Override
	public void buttonClick(ClickEvent event) {
		
		try {

			ContentPoolDef contentPoolDef = poolDefUiModule.getContentPoolDef();
			
			if(contentPoolDef != null){

                getMifssHolder().getStorageService().modifyContentPool(storageDef.getName(), contentPoolDef);
				parent.resetAllPanes();
				Notification.show(MifssMessages.getMessage("sys.success.pool.updated"));
                parentWindow.close();

			} else {
				Notification.show(MifssMessages.getMessage("sys.error.pool.def.not.defined"));
			}
			

		} catch (StorageServiceException e) {
			Notification.show(MifssMessages.getMessage("sys.error.pool.def.update") + " : " + e.getMessage());
		}

	}

}
